package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.domain.po.BookEntity;
import com.example.demo.domain.po.BorrowEntity;
import com.example.demo.service.BorrowService;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;
    @PostMapping("/getAllBorrow")
    public R getAllBorrow() {
        List<BorrowEntity> entitys = borrowService.getBorrows();
        return R.success(entitys);
    }

    @PostMapping("/deleteBorrow")
    public R deleteBorrow(@RequestBody BorrowEntity borrowEntity) {
        System.out.println(borrowEntity.getEnd() + "=================================================");
        int x = borrowService.deleteBorrow(borrowEntity);
        String 	str = "delete success";
        return R.success(str);
    }
    @PostMapping("/addBorrow")
    public R addBorrow(@RequestBody BorrowEntity borrowEntity){
        BorrowEntity entity = borrowService.addBorrow(borrowEntity);
        return R.success(entity);
    }
    @PostMapping("/updateBorrowEnd")
    public R updateBorrowEnd(@RequestBody JSONObject params){
        int id = params.getInteger("id");
        int day = params.getInteger("day");
        System.out.println("id = " + id + "day = " + day + "  ===================================");
        boolean status = borrowService.updateBorrowEnd(id, day);
        return R.success(status);
    }
    @PostMapping("/getBorrowsByUserName")
    public R getBorrowsByUserName(@RequestBody String username){
        List<BorrowEntity> entitys = borrowService.getBorrowsByUserName(username);
        return R.success(entitys);
    }
    @PostMapping("/getBorrowsByBookID")
    public R getBorrowsByBookID(@RequestBody Long bookID){
        List<BorrowEntity> entitys = borrowService.getBorrowsByBookID(bookID);
        return R.success(entitys);
    }
    @PostMapping("/getBorrowsByBookName")
    public R getBorrowsByBookName(@RequestBody String bookname){
        List<BorrowEntity> entitys = borrowService.getBorrowsByBookName(bookname);
        return R.success(entitys);
    }
}
